const transferMenu = require('./router/bds/transfer.js');
const funcApi = require('./router/bds/funcApi.js');

cmdRegister();
function cmdRegister(){
    var cmd = mc.newCommand("sync", "WS协调服务端功能", PermType.Any); //PermType.Any: 任何人,  PermType.GameMasters: 仅OP, PermType.Console: 仅控制台
    cmd.setEnum("SyncListAction", ["list"]);
    cmd.setEnum("SyncListNumAction", ["num"]);
    cmd.setEnum("SyncTransferAction", ["transfer"]);
    cmd.setEnum("SyncStopAction", ["stop"]);
    cmd.setEnum("SyncChangeAction", ["debug"]);


    cmd.mandatory("action", ParamType.Enum, "SyncListAction", 1);
    cmd.optional("listnum", ParamType.Enum, "SyncListNumAction", 1);
    cmd.mandatory("action", ParamType.Enum, "SyncTransferAction", 1);
    cmd.mandatory("action", ParamType.Enum, "SyncStopAction", 1);
    cmd.mandatory("action", ParamType.Enum, "SyncChangeAction", 1);


    cmd.mandatory("debugMode", ParamType.Bool);
    cmd.mandatory("BDSIp", ParamType.String);
    cmd.mandatory("BDSPort", ParamType.Int);
    cmd.mandatory("realName", ParamType.String);

    cmd.overload(["SyncStopAction"]);
    cmd.overload(["SyncListAction", "listnum"]);
    cmd.overload(["SyncTransferAction"]);
    cmd.overload(["SyncTransferAction", "BDSIp", "BDSPort"]);
    cmd.overload(["SyncChangeAction", "debugMode"]);


    cmd.setCallback((_cmd, _ori, _out, _res) => {
        //logger.debug(`命令执行的主体: ${_ori.type}`); 玩家：0 控制台 7
        let pl = _ori.player;
        if (_res.action == 'list'){
            if (_res.listnum != undefined){
                _out.success(String(funcApi.getOnlinePlayersNum()));
            }else{
                
                _out.success(funcApi.getOnlinePlayersName());
            }
            return;
        }
        
        if (_res.action == 'transfer'){
            if (pl == null){
                _out.error('此命令仅允许玩家执行');
                return;
            }
            if (_res.BDSIp != null && _res.BDSPort != null){
                pl.transServer(_res.BDSIp, _res.BDSPort);
                return;
            }
            transferMenu(pl);
            return;
        }

        if (_res.action == 'stop'){
            if (!(pl == null || pl.isOP() == true)){
                _out.error('此命令仅管理员可执行');
                return;
            }
            syncstop();
            return;
        }

        if (_res.action == 'debug'){
            if (!(pl == null || pl.isOP() == true)){
                _out.error('此命令仅管理员可执行');
                return;
            }
            if (_res.debugMode == true){
                CONF.CONFIG.set('Websocket', {...CONF.CONFIG.get('Websocket'), Debug: true});
                WSC.logger.setConsole(true, 5);
                _out.success('WS协调客户端 Debug模式开启');
            }else{
                CONF.CONFIG.set('Websocket', {...CONF.CONFIG.get('Websocket'), Debug: false});
                WSC.logger.setConsole(true, 4); 
                _out.success('WS协调客户端 Debug模式关闭');
            }
        }
    });
    cmd.setup();
}

function syncstop(){
    logger.warn("######## 协调服务端安全退出进程 ########");
    mc.broadcast(`[${CONF.CONFIG.get('Websocket')['ServerName']}] §c十五秒后关服维护`);
    let count = 15;
    let taskID = setInterval(()=>{
        mc.broadcast(`§c---- §e${count} 秒后关服维护 §c----`);
        logger.warn(`---- ${count} 秒后踢出玩家 ----`);
        count--;
        if (count <= 0){
            mc.getOnlinePlayers().forEach((pl)=>{
                pl.disconnect(`[${CONF.CONFIG.get('Websocket')['ServerName']}] 服主关服维护`);
            });
            logger.info("######## 五秒后关闭服务器 ########");
            setTimeout(()=>{
                mc.runcmdEx('stop');
            },5000);
            clearInterval(taskID);
        }
    },1000);
}

